// import ppt1 from '@/assets/ppt/ppt1.png'
// import ppt2 from '@/assets/ppt/ppt2.png'
// import ppt3 from '@/assets/ppt/ppt3.png'
import pptxgen from 'pptxgenjs'

const createPPT = (function() {
  function createPPT() {
    this.pres
    this.slide
    this.defaultData = [{
      name: '',
      labels: [],
      values: []
    }]
    this.defaultData2 = [{
      type: '',
      data: [
        {
          name: '',
          labels: [],
          values: []
        },
        {
          name: '',
          labels: [],
          values: []
        }
      ],
      options: {
        chartColors: ['ffffff'],
        barGrouping: 'standard'
      }
    },
    {
      type: '',
      data: [
        {
          name: '',
          labels: [],
          values: []
        }
      ],
      options: {
        chartColors: ['F38940']
      }
    }]
    this.ppt1
    this.ppt2
    this.ppt3
  }

  /**
     * 创建PPT对象
     */
  createPPT.prototype.initPPt = function() {
    this.pres = new pptxgen() // 创建PPT对象
  }
  /**
     * 新增一页PPT
     */
  createPPT.prototype.addPage = function() {
    this.slide = this.pres.addSlide() // 新建幻灯片
  }
  /**
     *
     * @param {Object} data  //
     * @param {Number} pictureNum
     */
  createPPT.prototype.addText = function(data, pictureNum) {
    // 这里用了静态资源的图片
    // 要不要直接BASE64编码了--就不会下载出现XHR异常

    let file_path = this.ppt1
    switch (pictureNum) {
      case 1:
        file_path = this.ppt1
        break
      case 2:
        file_path = this.ppt2
        break
      case 3:
        file_path = this.ppt3
        break
      default:
        break
    }
    this.slide.background = { data: file_path }
    data.forEach(ele => {
      this.slide.addText(ele.text, ele.style)
    })
    // 给幻灯片添加内容
  }

  /** 添加表格数据 */
  createPPT.prototype.addTable = function(data, opts) {
    try {
      this.slide.addTable(data, opts)
    } catch (error) {
      try {
        this.slide.addTable(this.defaultData, opts)
      } catch (error) {
        console.log(error)
      }
    }
  }

  /**
     * 添加柱状图
     */
  createPPT.prototype.addBar = function(data, opts) {
    try {
      this.slide.addChart(this.pres.ChartType.bar, data, opts)
    } catch (error) {
      try {
        this.slide.addChart(this.pres.ChartType.bar, this.defaultData, opts)
      } catch (error) {
        console.log(error)
      }
    }
  }

  /**
     * 添加折线图
     */
  createPPT.prototype.addline = function(data, opts) {
    try {
      this.slide.addChart(this.pres.ChartType.line, data, opts)
    } catch (error) {
      try {
        this.slide.addChart(this.pres.ChartType.line, this.defaultData, opts)
      } catch (error) {
        console.log(error)
      }
    }
  }
  /**
     * 添加饼图
     */
  createPPT.prototype.addPie = function(data, opts) {
    try {
      this.slide.addChart(this.pres.ChartType.pie, data, opts)
    } catch (error) {
      try {
        this.slide.addChart(this.pres.ChartType.pie, this.defaultData, opts)
      } catch (error) {
        console.log(error)
      }
    }
  }
  /**
     * 添加饼图空心
     */
  createPPT.prototype.addPieDOUGHNUT = function(data, opts) {
    try {
      this.slide.addChart(this.pres.ChartType.pie, data, opts)
    } catch (error) {
      try {
        this.slide.addChart(this.pres.ChartType.pie, this.defaultData, opts)
      } catch (error) {
        console.log(error)
      }
    }
  }
  /**
     * 添加组合图
     */
  createPPT.prototype.addCombo = function(data, opts) {
    try {
      this.slide.addChart(data, opts)
    } catch (error) {
      try {
        this.slide.addChart(this.defaultData2, opts)
      } catch (error) {
        console.log(error)
      }
    }
  }
  /**
     * 导出PPT
     */
  createPPT.prototype.exportPowerPoint = function(fileName) {
    return this.pres.writeFile({ fileName: fileName })
  }
  return createPPT
})()

export default createPPT
